﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

using OctoOS;

/// <summary>
/// Summary description for ArticleMeta
/// </summary>
[DataContract]
public class ArticleMeta
{
	[DataMember(Order = 1)]
	public long UserID { get; private set; }

	[DataMember(Order = 2)]
	public string Preface { get; internal set; }

	[DataMember(Order = 3)]
	public DateTime PublishStartTime { get; internal set; }

	[DataMember(Order = 4)]
	public string Author { get; internal set; }

	[DataMember(Order = 5)]
	public DateTime CreateTime { get; internal set; }

	[DataMember(Order = 6)]
	public DateTime PublishStopTime { get; internal set; }

	[DataMember(Order = 7)]
	public long[] NonCompletePages { get; set; }

	private ArticleMeta()
	{
		//default values
		PublishStartTime = DateTime.MinValue;
		CreateTime = DateTime.MinValue;
		PublishStopTime = DateTime.MaxValue;
		NonCompletePages = new long[0];
	}

	public ArticleMeta(string preface, DateTime publishStartTime, DateTime
		publishStopTime, long userID, string author, DateTime createTime)
	{
		Preface = preface;
		PublishStartTime = publishStartTime;
		PublishStopTime = publishStopTime;
		UserID = userID;
		Author = author;
		CreateTime = createTime;
		NonCompletePages = new long[0];
	}

	public bool PublishedNow
	{
		get
		{
			return PublishStartTime <= DateTime.UtcNow 
				&& PublishStopTime > DateTime.UtcNow;
		}
	}

    public void AddNonCompletedPageID(long newPageID)
    {
        List<long> newNonCompeted = new List<long>();
        foreach(var pageID in NonCompletePages)
            newNonCompeted.Add(pageID);

        newNonCompeted.Add(newPageID);
        NonCompletePages = newNonCompeted.ToArray();
    }

	public void DeleteNonCompletedPageID(long deletePageID)
	{
		List<long> newNonCompeted = new List<long>();
		foreach(var pageID in NonCompletePages)
		{
			if(pageID != deletePageID)
				newNonCompeted.Add(pageID);
		}

		NonCompletePages = newNonCompeted.ToArray();
	}


	public static ArticleMeta Create(byte[] array)
	{
		return ConvertUtility.FromProtoBuf<ArticleMeta>(array);
	}

	public byte[] ToArray()
	{
		return ConvertUtility.ToProtoBuf<ArticleMeta>(this);
	}
}